﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZSPG_ZJGSBInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_PGGS";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["ID"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
            }
            Persistence1.BindControl = panel1;
            BindData();
            IsLookPerson();
        }


        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btn_Insert = (Button)FindControl("btn_Insert");
                        btn_Insert.Visible = false;
                    }
                }
            }
        }


        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            Label 立项号 = (Label)FindControl("立项号");
            Label 项目名称 = (Label)FindControl("项目名称");
            Label 项目批文号 = (Label)FindControl("项目批文号");
            Label 评估机构 = (Label)FindControl("评估机构");
            TextBox 委托单位 = (TextBox)FindControl("委托单位");

            Label 房屋征收部门 = (Label)FindControl("房屋征收部门");
            Label 征收实施单位 = (Label)FindControl("征收实施单位");
            TextBox 技术负责人 = (TextBox)FindControl("技术负责人");
            Label 征收范围 = (Label)FindControl("征收范围");
            Label 区属 = (Label)FindControl("区属");

            TextBox 级别 = (TextBox)FindControl("级别");
            TextBox 选择评估机构方式 = (TextBox)FindControl("选择评估机构方式");
            Label 规划文号 = (Label)FindControl("规划文号");
            Label 项目地址 = (Label)FindControl("项目地址");

            TextBox txt概算补偿总金额 = (TextBox)FindControl("txt概算补偿总金额");

            string PID = HttpContext.Current.Request["PID"];
           
            if (!string.IsNullOrEmpty(PID))
            {
                TextBox txtPID = (TextBox)FindControl("txtPID");
                txtPID.Text = PID;
                EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(PID);

                立项号.Text = project.立项号;
                项目名称.Text = project.项目名称;
                项目批文号.Text = project.项目批文号;
                评估机构.Text = project.概算评估机构;
              //  委托单位.Text = project.评估委托单位;

                房屋征收部门.Text = project.征收部门;
                征收实施单位.Text = project.征收实施单位;
              //  技术负责人.Text = project.技术负责人;
                征收范围.Text = project.征收范围;
                区属.Text = project.区属;

               // 级别.Text = project.级别;
                选择评估机构方式.Text = project.选择评估机构方式;
                规划文号.Text = project.用地规划许可证号;
                项目地址.Text = project.项目地址;
            }
            string temp = txt概算补偿总金额.Text;
            if (string.IsNullOrEmpty(temp))
            {
                txt概算补偿总金额.Text = "0";
            }
        }
        protected void BindData() {
            Button btn_Insert = (Button)FindControl("btn_Insert");
            Button btnUpload = (Button)FindControl("btnUpload");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            btnUpload.Click += new EventHandler(btnUpload_Click);
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            if (!string.IsNullOrEmpty(Persistence1.KeyValue)) {
                string strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}');</script>",
                Persistence1.KeyValue, base.Page.Request["ID"].Trim(), base.Page.Request["PID"].Trim());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
            }
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            string strPID = base.Page.Request["PID"];
            /*校验是否存在附件，存在改变交换字段*/
            if (_EluZSDataProvider.CkFileCount(strPID, "3")) { _EluZSDataProvider.UpdateFileCS(strPID, "3"); }
            HttpContext.Current.Response.Redirect("ZSPG_ZJGSBInfo.aspx?ID=" + Persistence1.KeyValue+"&PID="+base.Page.Request["PID"]);
        }
    }
}


